package com.yuexunit.operationman.nbsy.module.OperationInventory.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;


import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.base.BaseKJActivity;
import com.yuexunit.operationman.nbsy.config.AppConfig;
import com.yuexunit.operationman.nbsy.module.OperationInventory.adapter.TempletGoodsAdapter;
import com.yuexunit.operationman.nbsy.module.OperationInventory.bean.GoodsBean;
import com.yuexunit.operationman.nbsy.module.OperationInventory.bean.InventoryBean;
import com.yuexunit.operationman.nbsy.module.OperationInventory.model.impl.GoodsModel;
import com.yuexunit.operationman.nbsy.module.OperationInventory.presenter.IGoodsPresenter;
import com.yuexunit.operationman.nbsy.module.OperationInventory.presenter.impl.GoodsPresenter;
import com.yuexunit.operationman.nbsy.module.OperationInventory.ui.view.IGoodsView;
import com.yuexunit.operationman.nbsy.widget.FixedSwipeListView;
import com.yuexunit.operationman.nbsy.widget.LoadingDialog;

import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2017/9/5.
 */
public class Act_OperationInventory_SelectTemplet extends BaseKJActivity implements IGoodsView{

    @Bind(R.id.tv_patient_name)
    TextView tvPatientName;
    @Bind(R.id.tv_patient_sex)
    TextView tvPatientSex;
    @Bind(R.id.tv_patient_visitno)
    TextView tvPatientVisitno;
    @Bind(R.id.tv_patient_disease)
    TextView tvPatientDisease;
    @Bind(R.id.listview)
    FixedSwipeListView listview;

    TempletGoodsAdapter adapter;
    IGoodsPresenter presenter;
    LoadingDialog loadingDialog;

    ArrayList<GoodsBean.ObjectEntity> objectEntities = new ArrayList<GoodsBean.ObjectEntity>();
    ArrayList<InventoryBean.ObjectEntity.CheckItemListEntity> checkItemListEntities = new ArrayList<InventoryBean.ObjectEntity.CheckItemListEntity>();

    @Override
    public void setRootView() {
        setContentView(R.layout.act_operationinventory_selecttemplet);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        setTitle();
        loadingDialog = new LoadingDialog(this);
        presenter = new GoodsPresenter(this,new GoodsModel());

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }

    @Override
    public void onResume(){
        super.onResume();
       if(getIntent().getExtras().getString("type").equals("2"))
       {
           Map<String, String> params = new HashMap<>();
           params.put("goodsTempleId",getIntent().getExtras().getInt("id")+"");
           presenter.getGoodsDate(params);
       }
    }



    private void setTitle() {
        tvPatientName.setText(AppConfig.INVENTORYBEAN.getObject().getCheck().getPatientName());
        if (AppConfig.INVENTORYBEAN.getObject().getCheck().getPatientSex().equals("1")) {
            tvPatientSex.setText("男");
        } else if (AppConfig.INVENTORYBEAN.getObject().getCheck().getPatientSex().equals("2")) {
            tvPatientSex.setText("女");
        } else {
            tvPatientSex.setText("不详");
        }
        tvPatientVisitno.setText("住院号" + AppConfig.INVENTORYBEAN.getObject().getCheck().getInHospitalNo());
        tvPatientDisease.setText(PreferenceHelper.readString(aty, AppConfig.PATIENT_INFO, AppConfig.PATIENT_DISEASE));
    }


    @OnClick({R.id.titlebar_docadvice_img_back, R.id.add})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_docadvice_img_back:
                Intent intent = new Intent(aty, Act_OperationInventory_Templet.class);
                aty.startActivity(intent);
                finish();
                break;
            case R.id.add:
                if(objectEntities.size() >0)
                {
                    objectEntities = adapter.getGoodsValue();
                    for(int i=0;i<objectEntities.size();i++){

                        if(objectEntities.get(i).getCount() >0) {
                            InventoryBean.ObjectEntity.CheckItemListEntity checkItemListEntity = new InventoryBean.ObjectEntity.CheckItemListEntity();
                            checkItemListEntity.setCheckId("");
                            checkItemListEntity.setCheckItemId("");
                            checkItemListEntity.setItemCode(objectEntities.get(i).getItemCode());
                            checkItemListEntity.setItemName(objectEntities.get(i).getItemName());
                            checkItemListEntity.setBeforeCheck(objectEntities.get(i).getCount());
                            checkItemListEntity.setInAdd("");
                            checkItemListEntity.setBeforeCloseBody(0);
                            checkItemListEntity.setAfterCloseBody(0);
                            checkItemListEntity.setAfterSkin(0);
                            checkItemListEntities.add(checkItemListEntity);
                        }
                    }
                }
                Bundle bundle = new Bundle();
                bundle.putSerializable("goods",checkItemListEntities);
                bundle.putBoolean("isFirst",true);
                Intent intent2 = new Intent(aty, Act_OperationInventory_GoodsList.class);
                intent2.putExtras(bundle);
                aty.startActivity(intent2);
                finish();
                break;
        }
    }

    @Override
    public void getGoodsDate(GoodsBean goodsBean) {
        if(goodsBean.getCode() == 200)
        {
            objectEntities = goodsBean.getObject();
            adapter = new TempletGoodsAdapter(listview,objectEntities,R.layout.item_operationinventory_goods);
            listview.setAdapter(adapter);
        }
    }

    @Override
    public void loadingDialogStatus(int status) {
        switch (status) {
            case AppConfig.SHOW_DIALOG:
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
                break;
            case AppConfig.DISMISS_DIALOG:
                loadingDialog.dismiss();
                break;

        }
    }
}
